package com.xlh.virtualization.dao.helper;

import com.xlh.virtualization.dao.ImageAllocationSlaveMapper;
import com.xlh.virtualization.domain.ImageAllocationSlave;
import com.xlh.virtualization.domain.example.ImageAllocationSlaveExample;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * @author cheer
 */
@Repository
public class ImageAllocationSlaveMapperHelper {

    @Autowired
    private ImageAllocationSlaveMapper imageAllocationSlaveMapper;

    public void deleteById(Long id) {
        ImageAllocationSlave imageAllocationSlave = ImageAllocationSlave.builder()
                .id(id)
                .deleted(true)
                .build();
        imageAllocationSlaveMapper.updateByPrimaryKeySelective(imageAllocationSlave);
    }

    public void deleteByContext(Integer contextType, Long contextId) {
        ImageAllocationSlaveExample example = new ImageAllocationSlaveExample();
        example.or().andContextTypeEqualTo(contextType)
                .andContextIdEqualTo(contextId)
                .andDeletedEqualTo(false);
        ImageAllocationSlave imageAllocationSlave = ImageAllocationSlave.builder()
                .deleted(true)
                .build();
        imageAllocationSlaveMapper.updateByExampleSelective(imageAllocationSlave, example);
    }

    public void updateImage(Integer contextType, Long contextId, Integer roleNumber, Long imageId) {
        ImageAllocationSlaveExample example = new ImageAllocationSlaveExample();
        example.or().andContextTypeEqualTo(contextType)
                .andContextIdEqualTo(contextId)
                .andRoleNumberEqualTo(roleNumber)
                .andDeletedEqualTo(false);
        ImageAllocationSlave imageAllocationSlave = ImageAllocationSlave.builder()
                .imageId(imageId)
                .build();
        imageAllocationSlaveMapper.updateByExampleSelective(imageAllocationSlave, example);
    }

    public List<ImageAllocationSlave> listByContext(Integer contextType, Long contextId) {
        ImageAllocationSlaveExample example = new ImageAllocationSlaveExample();
        example.or().andContextTypeEqualTo(contextType)
                .andContextIdEqualTo(contextId)
                .andDeletedEqualTo(false);
        example.setOrderByClause("role_number asc");
        return imageAllocationSlaveMapper.selectByExample(example);
    }

    public List<ImageAllocationSlave> listByContext(Integer contextType, List<Long> contextIds) {
        if (CollectionUtils.isEmpty(contextIds)) {
            return new ArrayList<>();
        }
        ImageAllocationSlaveExample example = new ImageAllocationSlaveExample();
        example.or().andContextTypeEqualTo(contextType)
                .andContextIdIn(contextIds)
                .andDeletedEqualTo(false);
        example.setOrderByClause("context_id asc, role_number asc");
        return imageAllocationSlaveMapper.selectByExample(example);
    }

    public void trueDeleteByContext(Integer contextType, Long contextId) {
        ImageAllocationSlaveExample example = new ImageAllocationSlaveExample();
        example.or().andContextTypeEqualTo(contextType)
                .andContextIdEqualTo(contextId);
        imageAllocationSlaveMapper.deleteByExample(example);
    }
}
